package com.liang.paul.officelite.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.codetroopers.betterpickers.calendardatepicker.CalendarDatePickerDialogFragment;
import com.codetroopers.betterpickers.radialtimepicker.RadialTimePickerDialogFragment;
import com.dd.processbutton.iml.ActionProcessButton;
import com.liang.paul.officelite.R;
import com.liang.paul.officelite.beams.TaskBeam;

import java.util.Calendar;

import cn.bmob.v3.listener.SaveListener;

public class AddTaskActivity extends FastActivity {


    EditText name;
    TextView date, time;
    ActionProcessButton btn_add;
    boolean isToday = false;
    String teamId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_task);
        teamId = getIntent().getStringExtra("teamId");
        initView();
    }

    private void initView(){
        name = f(R.id.editText_addtask_name);
        date = f(R.id.txt_pickup_date);
        time = f(R.id.txt_pickup_time);
        btn_add = f(R.id.btn_addtask_add);
        date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CalendarDatePickerDialogFragment cdp = new CalendarDatePickerDialogFragment()
                        .setOnDateSetListener(new CalendarDatePickerDialogFragment.OnDateSetListener() {
                            @Override
                            public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
                                Calendar calendar = Calendar.getInstance();
                                if(year > calendar.get(Calendar.YEAR)
                                        || (year == calendar.get(Calendar.YEAR) && monthOfYear > calendar.get(Calendar.MONTH))
                                        || (year == calendar.get(Calendar.YEAR)
                                        && monthOfYear == calendar.get(Calendar.MONTH)
                                        && dayOfMonth > calendar.get(Calendar.DAY_OF_MONTH))){
                                    date.setText("" + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                                    isToday = false;
                                }else if(year == calendar.get(Calendar.YEAR)
                                        && monthOfYear == calendar.get(Calendar.MONTH)
                                        && dayOfMonth == calendar.get(Calendar.DAY_OF_MONTH)){
                                    date.setText("" + year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                                    isToday = true;
                                }else {
                                    Toast.makeText(getApplicationContext(), "请选择未来的时间", Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setFirstDayOfWeek(Calendar.SUNDAY)
                        .setPreselectedDate(Calendar.getInstance().get(Calendar.YEAR),
                                Calendar.getInstance().get(Calendar.MONTH),
                                Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
                cdp.show(getSupportFragmentManager(), "DATE_PIKER");
            }
        });

        time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new RadialTimePickerDialogFragment()
                        .setOnTimeSetListener(new RadialTimePickerDialogFragment.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(RadialTimePickerDialogFragment dialog, int hourOfDay, int minute) {
                                if (!isToday) {
                                    time.setText(hourOfDay + ":" + minute);
                                } else {
                                    Calendar calendar = Calendar.getInstance();
                                    if (calendar.get(Calendar.HOUR) < hourOfDay
                                            || (calendar.get(Calendar.HOUR) == hourOfDay && calendar.get(Calendar.MINUTE) < minute)) {
                                        time.setText(hourOfDay + ":" + minute);
                                    } else {
                                        Toast.makeText(getApplicationContext(), "请选择未来的时间", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        })
                        .setForced24hFormat()
                        .setStartTime(Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE))
                        .show(getSupportFragmentManager(), "TIME_PICKER");
            }
        });

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(name.getText().toString().equals("") || date.getText().toString().equals("选择截止日期")
                        || time.getText().toString().equals("选择截止时间")){
                    Toast.makeText(getApplicationContext(), "选项未完成", Toast.LENGTH_SHORT).show();
                }else {
                    btn_add.setMode(ActionProcessButton.Mode.ENDLESS);
                    btn_add.setProgress(1);
                    TaskBeam beam = new TaskBeam();
                    beam.setTeamId(teamId);
                    beam.setDDLDay(date.getText().toString());
                    beam.setDDLTime(time.getText().toString());
                    beam.setTitle(name.getText().toString());
                    beam.save(getApplicationContext(), new SaveListener() {
                        @Override
                        public void onSuccess() {
                            btn_add.setProgress(100);
                            setResult(112);
                            finish();
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }
}
